package com.jtjrenren.android.taxi.passenger.core.order.place;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.core.adapter.PlaceListAdapter;
import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseMvpActivity;
import com.jtjrenren.android.taxi.passenger.bean.AddressInfo;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.SpaceItemDecoration;
import com.library.weidget.ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Administrator on 2016/1/13.
 * 若将地址信息传参数进来 则先对坐标进行反地理编码 获取poi热点  当修改输入信息以后 则用建议
 */
public class PlaceChooseActivity extends BaseMvpActivity<PlaceChoosePresenter> implements PlaceChooseView {
    public static String EXTRA_ADDR = "address";
    public static String EXTRA_SHOWUSED = "showused"; //是否展示选择常用
    public static String INTENT_DATA_ADDR = "addr_selected";

    public static final int CODE_HOME = 100;
    public static final int CODE_COMPANY = 101;
    public static final int CODE_OTHER = 102;

    @BindView(R.id.place_choose_list)
    RecyclerView rcy_suglist;
    @BindView(R.id.place_choose_key)
    ClearEditText et_key;
    @BindView(R.id.addr_used_home)
    LinearLayout ll_home;
    @BindView(R.id.addr_used_company)
    LinearLayout ll_company;
    @BindView(R.id.addr_used_other)
    LinearLayout ll_other;
    @BindView(R.id.addr_used_home_desc)
    TextView tv_home;
    @BindView(R.id.addr_used_company_desc)
    TextView tv_company;
    @BindView(R.id.addr_used_other_desc)
    TextView tv_other;
    @BindView(R.id.title)
    TextView tv_title;

    private PlaceListAdapter adapter;


    public static Intent getDiyIntent(Context context, AddressInfo addressInfo, boolean isShowUsed) {
        Intent intent = new Intent(context, PlaceChooseActivity.class);
        if (addressInfo != null)
            intent.putExtra(EXTRA_ADDR, addressInfo);
        intent.putExtra(EXTRA_SHOWUSED, isShowUsed);
        return intent;
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_palce_choose;
    }


    @Override
    public PlaceChoosePresenter createPresenter() {
        return new PlaceChoosePresenterImpl(this);
    }

    @Override
    public void initM() {
        presenter.init();
        tv_title.setText(R.string.weizhixuanze);

        InitView.initVritalReclyView(this, rcy_suglist);
        rcy_suglist.addItemDecoration(new SpaceItemDecoration(0, 0, 0, 2));

        adapter = new PlaceListAdapter(this);
        adapter.setListener(new PlaceListAdapter.ItemClickListener() {
            @Override
            public void onItemClick(AddressInfo info) {
                presenter.selectedAddr(-1, info);
            }
        });
        rcy_suglist.setAdapter(adapter);

        if (getIntent().hasExtra(EXTRA_ADDR))
            presenter.initAddr((AddressInfo) getIntent().getExtras().get(EXTRA_ADDR));
        else
            presenter.initAddr(null);

        if (getIntent().hasExtra(EXTRA_SHOWUSED))
            presenter.initUsedShow(getIntent().getBooleanExtra(EXTRA_SHOWUSED, false));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destory();
    }

    @Override
    public void showUsedView() {
        ll_company.setVisibility(View.VISIBLE);
        ll_home.setVisibility(View.VISIBLE);
        ll_other.setVisibility(View.VISIBLE);
    }

    @Override
    public void refreshKey(String key) {
        et_key.setText(key);
    }

    @Override
    public void refreshHome(String addr) {
        tv_home.setText(addr);
    }

    @Override
    public void refreshCompany(String addr) {
        tv_company.setText(addr);
    }

    @Override
    public void refreshOther(String addr) {
        tv_other.setText(addr);
    }

    @Override
    public void refreshAddrList(List<AddressInfo> addressInfos) {
        if (adapter != null)
            adapter.replace(addressInfos);
    }

    @Override
    public void onFinish(AddressInfo info) {
        Intent data = new Intent();
        data.putExtra(INTENT_DATA_ADDR, info);
        setResult(RESULT_OK, data);

        finish();
    }

    @Override
    public void chooseHome() {
        startActivityForResult(PlaceChooseActivity.getDiyIntent(this, null, false), CODE_HOME);
    }

    @Override
    public void chooseCompany() {
        startActivityForResult(PlaceChooseActivity.getDiyIntent(this, null, false), CODE_COMPANY);
    }

    @Override
    public void chooseOther() {
        startActivityForResult(PlaceChooseActivity.getDiyIntent(this, null, false), CODE_OTHER);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CODE_COMPANY && resultCode == RESULT_OK) {
            AddressInfo info = (AddressInfo) data.getSerializableExtra(PlaceChooseActivity.INTENT_DATA_ADDR);
            presenter.refreshAddrSave(Constants.CONFIG_ADDR_TYPE_COMPANY, info);
        } else if (requestCode == CODE_HOME && resultCode == RESULT_OK) {
            AddressInfo info = (AddressInfo) data.getSerializableExtra(PlaceChooseActivity.INTENT_DATA_ADDR);
            presenter.refreshAddrSave(Constants.CONFIG_ADDR_TYPE_HOME, info);
        } else if (requestCode == CODE_OTHER && resultCode == RESULT_OK) {
            AddressInfo info = (AddressInfo) data.getSerializableExtra(PlaceChooseActivity.INTENT_DATA_ADDR);
            presenter.refreshAddrSave(Constants.CONFIG_ADDR_TYPE_OTHER, info);
        }
    }

    @OnTextChanged(R.id.place_choose_key)
    void onTextChanged(CharSequence text) {
        String key = text.toString().trim();
        presenter.sugest(key);
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.addr_used_home_edit)
    public void clickHomeE() {
        chooseHome();
    }

    @OnClick(R.id.addr_used_company_edit)
    public void clickComE() {
        chooseCompany();
    }

    @OnClick(R.id.addr_used_other_edit)
    public void clickOtherE() {
        chooseOther();
    }

    @OnClick(R.id.addr_used_company)
    public void clickCompany() {
        presenter.selectedAddr(Constants.CONFIG_ADDR_TYPE_COMPANY, null);
    }

    @OnClick(R.id.addr_used_home)
    public void clickHome() {
        presenter.selectedAddr(Constants.CONFIG_ADDR_TYPE_HOME, null);
    }

    @OnClick(R.id.addr_used_other)
    public void clickOther() {
        presenter.selectedAddr(Constants.CONFIG_ADDR_TYPE_OTHER, null);
    }

}
